<template>
  <div class="container">
    <div class="header">
      <el-button icon="el-icon-plus" class="btn1" @click="addDishDialog"
        >新增菜品分类</el-button
      >
      <el-dialog
        :title="dishDialogTitle"
        :visible.sync="dishDialogVisible"
        width="30%"
      >
        <el-form :model="dishForm" label-width="100px">
          <el-form-item label="分类名称" prop="name">
            <el-input v-model="dishForm.name"></el-input>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input v-model="dishForm.sort"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dishDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="dishSubmit">确定</el-button>
        </span>
      </el-dialog>
      <el-button icon="el-icon-plus" class="btn2" @click="addComboDialog"
        >新增套餐分类</el-button
      >
      <el-dialog
        :title="comboDialogTitle"
        :visible.sync="comboDialogVisible"
        width="30%"
      >
        <el-form :model="comboForm" label-width="100px">
          <el-form-item label="分类名称" prop="name">
            <el-input v-model="comboForm.name"></el-input>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input v-model="comboForm.sort"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="comboDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="comboSubmit">确定</el-button>
        </span>
      </el-dialog>
    </div>
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="name" label="分类名称"> </el-table-column>
      <el-table-column prop="type" label="分类类型">
        <template v-slot="{ row }">
          <span>{{ row.type === 1 ? "菜品分类" : "套餐分类" }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="操作时间"> </el-table-column>
      <el-table-column prop="sort" label="排序"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link type="primary" @click="editDialog(row)">编辑</el-link>
          <el-dialog
            :title="editDialogTitle"
            :visible.sync="editDialogVisible"
            width="30%"
          >
            <el-form :model="editForm" label-width="100px">
              <el-form-item label="分类名称" prop="name">
                <el-input v-model="editForm.name"></el-input>
              </el-form-item>
              <el-form-item label="排序" prop="sort">
                <el-input v-model="editForm.sort"></el-input>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="editDialogVisible = false">取消</el-button>
              <el-button type="primary" @click="editSubmit">确定</el-button>
            </span>
          </el-dialog>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm title="确认删除该分类?" @onConfirm="handleDelete(row)">
            <el-link slot="reference" type="primary">删除</el-link>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="footer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  getCategorys,
  addCategory,
  updateCategory,
  deleteCategory,
} from "@/api/category";
export default {
  data() {
    return {
      input: "",
      tableData: [],
      currentPage4: 4,
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      dialogVisible: false,
      dishDialogVisible: false,
      comboDialogVisible: false,
      editDialogVisible: false,
      dishForm: {
        name: "",
        sort: "",
        type: 1,
      },
      comboForm: {
        name: "",
        sort: "",
        type: 2,
      },
      editForm: {
        id: "",
        name: "",
        sort: "",
        type: "",
      },
      dishDialogTitle: "新增菜品分类",
      comboDialogTitle: "新增套餐分类",
      editDialogTitle: "编辑分类",
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      getCategorys(this.pageInfo).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1;
      this.pageInfo.pageSize = val;
      this.init();
    },
    addDishDialog() {
      this.dishForm = { name: "", sort: "", type: 1 };
      this.dishDialogVisible = true;
    },
    addComboDialog() {
      this.comboForm = { name: "", sort: "", type: 2 };
      this.comboDialogVisible = true;
    },
    editDialog(row) {
      this.editForm = { ...row };
      this.editDialogVisible = true;
    },
    dishSubmit() {
      addCategory(this.dishForm).then((res) => {
        this.$message.success("新增菜品分类成功");
        this.dishDialogVisible = false;
        this.init();
      });
    },
    comboSubmit() {
      addCategory(this.comboForm).then((res) => {
        this.$message.success("新增套餐分类成功");
        this.comboDialogVisible = false;
        this.init();
      });
    },
    editSubmit() {
      updateCategory(this.editForm).then((res) => {
        this.$message.success("编辑成功");
        this.editDialogVisible = false;
        this.init();
      });
    },
    //报错Uncaught TypeError: Cannot read properties of undefined (reading '0')
    // at HTMLBodyElement.<anonymous> (all.js:50:92234)
    handleDelete(row) {
      deleteCategory(row.id).then((res) => {
        this.$message.success("删除成功");
        this.init();
      });
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 30px;
  .header {
    .btn1 {
      background-color: #333333;
      color: #fff;
    }
    .btn2 {
      background-color: #ffc200;
      color: #333333;
    }
    .el-input {
      width: 250px;
      i {
        cursor: pointer;
      }
    }
  }
  .el-table {
    margin-top: 20px;
  }
  .footer {
    margin-top: 20px;
    display: flex;
    justify-content: center;
  }
}
</style>
